<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/3
 * Time: 13:46
 */
namespace app\api\controller;

use think\Controller;
use think\Request;
use app\api\model\Coupon as ProductModel;
use app\api\model\CouponMember;


class Coupon extends Controller{

    private $pro;

    public function __construct(Request $request = null)
    {
        $this->pro = new ProductModel();
    }

    /**
     * Notes:  添加优惠券
     * Create Date: 2018/10/3
     * Create by :lijie
     */
    public function addCoupon(Request $request)
    {
        $param = $request->post();
        $vali  = $this->validate($param,"Coupon.addCoupon");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        $param['ctime'] = time();
        $param['utime'] = time();
        $param['redeemcode'] = $param['c_id']."orange".substr(uniqid(),-5);
        $res = $this->pro->addCoupon($param);
        if($res){
            return apiResponse(200,'添加成功',$res);
        }else {
            return apiResponse(201,"添加失败",$res);
        }
    }

    /**
     * Notes:  删除优惠券
     * Create Date: 2018/10/3
     * Create by :lijie
     */
    public function delCoupon()
    {
        $id = input('post.id');
        if(empty($id)){
            return apiResponse(201,"id不能为空",'');
        }
        //
        $res = $this->pro->delCoupon($id);
        if($res){
            return apiResponse(200,'删除成功',$res);
        }else {
            return apiResponse(201,"删除失败",$res);
        }
    }

    /**
     * Notes:  编辑优惠券
     * Create Date: 2018/10/3
     * Create by :lijie
     */
    public function editCoupon(Request $request)
    {
        $param = $request->post();
        $vali  = $this->validate($param,"Coupon.editCoupon");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        //
        $where['coupon_id'] = $param['coupon_id'];
        $where['status'] = array("NEQ",9);
        $param['utime'] = time();
        $res = $this->pro->editCoupon($param,$where);
        if($res){
            return apiResponse(200,'编辑成功',$res);
        }else {
            return apiResponse(201,"编辑失败",$res);
        }
    }

    /**
     * Notes:  查看优惠券
     * Create Date: 2018/10/3
     * Create by :lijie
     */
    public function couponList(Request $request)
    {
        $param = $request->post();
        $vali  = $this->validate($param,"Coupon.couponList");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
       // 洗车门店筛选
        empty($param['c_id']) ?  : $where['c_id'] = $param['c_id'];
        // 商品名称筛选
        empty($param['title']) ?  : $where['title'] = array('like',"%".$param['title']."%");
        // 状态筛选
        $where['status'] = array("NEQ",9);
        if(isset($param['status']) && $param['status']!=""){
            $where['status'] = $param['status'];
        }
        // 时间筛选
        if (!empty($param['start']) && !empty($param['end'])){
            $start = strtotime($param['start']." 00:00:00");
            $end   = strtotime($param['end']." 23:59:59");
            $where['ctime'] = array(array("EGT",$start),array("ELT",$end),'and');
        }
        //
        $res['list'] = $this->pro->couponList($where,$param['pagenum'],$param['listNum']);
        $res['total'] = $this->pro->where($where)->count();
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,"获取失败",$res);
        }
    }

    /**
     * Notes:  查看优惠券使用记录
     * Create Date: 2018/10/3
     * Create by :lijie
     */
    public function couponLog(Request $request)
    {
        $param = $request->post();
        $vali  = $this->validate($param,"Coupon.couponLog");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        $where['status'] = array("NEQ",9);
        if(isset($param['status']) && $param['status']!=""){
            $where['status'] = $param['status'];
        }
        // 时间筛选
        if (!empty($param['start']) && !empty($param['end'])){
            $start = strtotime($param['start']." 00:00:00");
            $end   = strtotime($param['end']." 23:59:59");
            $where['ctime'] = array(array("EGT",$start),array("ELT",$end),'and');
        }
        //
        $pro = new CouponMember();
        if (empty($param['pagenum'])) {
           $param['pagenum'] = 1;
        }
        if (empty($param['listNum'])) {
           $param['listNum'] = 20;
        }
        $res['list'] = $pro->couponList($where,$param['pagenum'],$param['listNum']);
        $res['total'] = $pro->where($where)->count();
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,"获取失败",$res);
        }
    }

    /**
     * Notes:  优惠券详情
     * Create Date: 2018/10/3
     * Create by :lijie
     */
    public function couponInfo()
    {
        $id['coupon_id'] = input('post.id');
        if(empty($id)){
            return apiResponse(201,"id不能为空",'');
        }
        //
        $res = $this->pro->couponInfo($id);
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,"获取失败",$res);
        }
    }

    /**
     * @author topqiang
     * @DateTime 2018-08-31T13:52:04+0800
     * Notes:  用户通过兑换码兑换
     */
    public function addCouponToUser(Request $request)
    {
        $param = $request->post();
        $vali  = $this->validate($param,"Coupon.addCouponToUser");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        $couponMember = new CouponMember();
        $hascoupon = $couponMember -> couponMemberInfo($param);
        if (!empty($hascoupon)) {
            return apiResponse(201,'您已领取过该优惠券！');
        }
        $w['redeemcode'] = $param['redeemcode'];
        $couponobj = $this->pro->couponInfo($w);
        if (empty($couponobj)) {
            return apiResponse(201,'兑换码不存在！');
        }
        $data = array(
            'coupon_id' => $couponobj['coupon_id'],
            'u_id' => $param['u_id'],
            'c_id' => $couponobj['c_id'],
            'end_time' => $couponobj['end_time'],
            'type' => $couponobj['type'],
            'start_time' => $couponobj['start_time'],
            'title' => $couponobj['title'],
            'desc' => $couponobj['desc'],
            'money' => $couponobj['money'],
            'min_price' => $couponobj['min_price'],
            'max_price' => $couponobj['max_price'],
            'redeemcode' => $param['redeemcode']
            );
        $res = $couponMember -> addCouponMember($data);
        if($res){
            return apiResponse(200,'兑换成功',$res);
        }else {
            return apiResponse(201,"兑换失败",$res);
        }
    }

    /**
     * 获取我的优惠券列表
     * @author topqiang
     * @DateTime 2018-09-01T08:45:05+0800
     * @param    Request                  $request [description]
     * @return   [type]                            [description]
     */
    public function getMyCoupon(Request $request)
    {
        $param = $request->post();
        $vali  = $this->validate($param,"Coupon.getMyCoupon");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        $where['u_id'] = $param['u_id'];
        // 状态筛选
        if ($param['status'] == 2) {
            $where['status'] = 0;
            $where['end_time'] = array("LT",date("Y-m-d",time()));
        }else{
            $where['status'] = $param['status'];
        }
        if ( !empty($param['c_id']) ) {
            $where['c_id'] = $param['c_id'];
        }
        $couMem = new CouponMember();
        $res['list'] = $couMem->getMyCoupon($where,$param['pagenum'],$param['listNum']);
        $res['total'] = $couMem->where($where)->count();
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,"获取失败",$res);
        }
    }
}